<?php

/**
 * This is the model class for table "{{Posts}}".
 *
 * The followings are the available columns in table '{{Posts}}':
 * @property string $post_id
 * @property string $title
 * @property string $desc
 * @property string $content
 * @property string $picture
 * @property integer $status
 * @property integer $focus
 * @property string $created
 * @property integer $type
 */
class Posts extends CActiveRecord {

    const POST_TYPE_ABOUTUS = 0;
    const POST_TYPE_WARRANTY = 1;
    const POST_TYPE_NEWS = 2;
    const POST_TYPE_PRODUCT = 3;
    const POST_TYPE_VIDEO=4;
    const
            POST_STATUS_ENABLE = 1,
            POST_STATUS_DISABLE = 0;
    const
            POST_IS_FOCUS = 0,
            POST_NO_FOCUS = 1;
    public $upload_dir;
    public $image;

    public function init() {
        $this->upload_dir = dirname(Yii::app()->basePath) . DIRECTORY_SEPARATOR . 'upload';
    }

    public static function getIsFocus() {
        return array(
            self::POST_NO_FOCUS => 'Bài viết bình thường',
            self::POST_IS_FOCUS => 'Bài viết nổi bật',
        );
    }

    public static function getTypePost() {
        return array(
            self::POST_TYPE_ABOUTUS => 'Giới thiệu',
            self::POST_TYPE_WARRANTY => 'Chính sách bảo hành',
            self::POST_TYPE_NEWS => 'Tin tức',
            self::POST_TYPE_PRODUCT => 'Sản phẩm',
            self::POST_TYPE_VIDEO=>'Video'
        );
    }

    public static function getStatusPost() {
        return array(
            self::POST_STATUS_ENABLE => 'Hiển thị',
            self::POST_STATUS_DISABLE => 'Không hiển thị',
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Posts the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{Posts}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title', 'required'),
            array('status, focus, type', 'numerical', 'integerOnly' => true),
            array('title, keywords, description', 'length', 'max' => 500),
//            array('image', 'file', 'types' => 'jpg,gif,png'),
            array('desc, content, created,slug,cat_id', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('post_id, title, desc, content, picture, status, focus, created, type', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'cat' => array(self::BELONGS_TO, 'Categrory', 'cat_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'post_id' => 'Id',
            'title' => 'Tiêu đề',
            'desc' => 'Chú thích',
            'content' => 'Nội dung',
            'picture' => 'Ảnh',
            'status' => 'Trạng thái',
            'focus' => 'Bài viết nổi bật',
            'created' => 'Thời gian',
            'type' => 'Kiểu bài',
            'slug' => 'Tiều đề url',
            'cat_id' => 'Thư mục',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('post_id', $this->post_id, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('desc', $this->desc, true);
        $criteria->compare('content', $this->content, true);
        $criteria->compare('picture', $this->picture, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('focus', $this->focus);
        $criteria->compare('created', $this->created, true);
        $criteria->compare('type', $this->type);
        $criteria->compare('cat_id', $this->cat_id);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'Pagination' => array(
                'PageSize' => 50,
            ),
        ));
    }

    public function getUrlByProduct() {
        return Yii::app()->createUrl('product/detail', array('url' => $this->post_id . '-' . $this->slug));
    }

    public function getUrlVideo(){
        return Yii::app()->createUrl('video/detail', array('url' => $this->post_id . '-' . $this->slug));
    }
    /*
     * @TODO: code url photo
     */
    public function getUrlPhoto() {
        return Yii::app()->baseUrl . '/upload/' . $this->post_id . '_' . $this->picture;
    }

    public function getUrlNews() {
        return Yii::app()->createUrl('news/detail', array('url' => $this->post_id . '-' . $this->slug));
    }

    private function deleteImage() {
        vH_Utils::rmdir_($this->upload_dir . DIRECTORY_SEPARATOR . $this->post_id);
    }

}